import React from 'react';
import { Modal, Form, Input, notification } from 'antd';
import { FormComponentProps } from 'antd/es/form';
import { editStudents, studentsList } from '@/services/api';
import { SIZE, PAGE } from '@/utils/constant';

interface props {
  onClose: () => void;
  dispatch: any;
}

interface UserFormProps extends FormComponentProps {
  ref: any;
}

const ref: any = React.createRef();
const { Item } = Form;
const MyForm = Form.create<UserFormProps>()(({ form: { getFieldDecorator } }: any) => {
  return (
    <Form layout="horizontal">
      <Item label="退学原因">
        {getFieldDecorator('out_reason')(<Input.TextArea placeholder="请输入" />)}
      </Item>
    </Form>
  );
});

const OutModal = ({ onClose, dispatch }: props) => {
  const studentsData = JSON.parse(sessionStorage.getItem('studentsData') || '');
  return (
    <Modal
      visible
      onCancel={onClose}
      maskClosable={false}
      title="退学信息"
      onOk={() => {
        ref.current.validateFields({ first: true }, (err: any, val: any) => {
          if (!err) {
            const out_reason = (val.out_reason || '').trim();
            const newVal: any = {};
            const residence_images: any = [];
            const health_images: any = [];
            // 新增退学信息
            newVal.extra = studentsData.extra || {};
            newVal.extra.is_out = 1;
            newVal.extra.out_reason = out_reason;
            studentsData.residences.forEach((item: any) => {
              residence_images.push(item.id);
            });
            studentsData.healths.forEach((item: any) => {
              health_images.push(item.id);
            });
            newVal.residence_images = residence_images;
            newVal.health_images = health_images;
            delete studentsData.parent;
            delete studentsData.teacher;
            delete studentsData.clase;
            delete studentsData.image;
            delete studentsData.residences;
            delete studentsData.healths;

            editStudents({ ...studentsData, ...newVal }).then((resp: any) => {
              if (resp.status === 'success') {
                notification.success({ message: '退学成功' });
                onClose();
                dispatch({
                  type: 'OPEN_LOADING',
                });
                studentsList({ page: PAGE, size: SIZE })
                  .then((res: any) => {
                    if (res.status === 'success') {
                      dispatch({
                        type: 'GET_DATA',
                        payload: res.data.data,
                      });
                      dispatch({
                        type: 'CHANGE_PAGINATION',
                        payload: {
                          current: PAGE,
                          pageSize: SIZE,
                          total: res.data.meta.total,
                        },
                      });
                    }
                    dispatch({
                      type: 'CLOSED_LOADING',
                    });
                  })
                  .catch(() => {
                    dispatch({
                      type: 'CLOSED_LOADING',
                    });
                  });
              }
            });
          }
        });
      }}
    >
      <MyForm ref={ref} />
    </Modal>
  );
};

export default OutModal;
